package com.ncmmall.api.apis.mall;

import com.ncmmall.api.apis.service.Result;
import com.ncmmall.api.security.Securitys;
import com.ncmmall.domain.business.trading.TradingLog;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 钱包记录列表
 *
 * @author likaikai
 * @date 2019-07-18 21:06
 */
@RestController
@RequestMapping("/balance")
public class BalanceApi {

    private Logger logger = LoggerFactory.getLogger(BalanceApi.class);

    @Autowired
    private QueryChannelService queryChannelService;


    /**
     * 记录列表
     *
     * @param request
     * @return
     */
    @GetMapping("/list")
    public Result list(HttpServletRequest request, @RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "20") int pageSize) {
        Result result = new Result();
        try {
            WebUser member = Securitys.user(request);

            QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageNo).setPageSize(pageSize);
            QueryPage<TradingLog> page = queryChannelService.queryPageEntity("select * from mall_trading_log where merchant_id = :mid order by createTime desc", pageRequest.setParams(ImmutableMap.of("mid", member.getId())), TradingLog.class);

            return result.ok().inject(ImmutableMap.of("page", page, "balance", member.getBalance()));

        } catch (Exception e) {
            logger.error("[获取钱包列表异常]", e);
            return result.fail().setMsg("网络请求异常");
        }
    }
}
